/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package Storage;

import java.util.HashMap;
import java.util.Vector;

/**
 * Object with two Strings: article and meaning.
 * @author Christoph Schnack
 *
 */
public class Meaning{
	String article,meaning,note="",category="n.a.",exampleSentence="",phonetic="~";
	Vector<Term> synonyms = new Vector<Term>();;
	HashMap<String,String> metaDescription=new HashMap<String,String>();
	
	public Meaning(){
		
	}
	
	public Meaning(String art, String mean){
		article = art;
		meaning = mean;
	}
	/**
	 * 
	 * @param art article
	 * @param mean meaning
	 * @param phon phonetic notation
	 * @param not note
	 * @param cat category
	 * @param eClause example clause
	 */
	//, String phon
	public Meaning(String art,String mean, String not, String cat, String eClause){
		meaning=mean;
		article=art;
		//phonetic = phon;
		note = not;
		setExampleSentence(eClause);
		setCategory(cat);
	}
	
	public void setMeaning(String arg){
		meaning=arg;
	}
	/**
	 * @return only the meaning without article
	 */
	public String getMeaning(){
		return meaning;
	}
	/**
	 * @return Meaining with article and category if existing respectively
	 */
	
	public Term getMeaningAsTerm(){
		return new Term(article,meaning,category);
	}
	
	/**
	 * Returns article only if existing.
	 * @author Christoph Schnack
	 * @return (article) meaning
	 */
	public String getMeaningWithArticle(){
		if(this.hasArticle()) return "("+article + ") " + meaning;
		else return meaning;
	}
	
	/**
	 * Returns article only if existing.
	 * @author Christoph Schnack
	 * @return article meaning
	 */
	public String getMeaningWithArticleUnformatted(){
		if(this.hasArticle()) return article + " " + meaning;
		else return meaning;
	}
	
	/**
	 * 
	 * @return (article) meaning [category] or empty string
	 */
	public String getMeaningAsStringFull(){
		return (hasArticle()?"("+article + ") ":"") + meaning + (hasCategory()?" {"+category+"}":"");
	}
	
	public Vector<String> getAllTerms(){
		Vector<String> all = new Vector<String>();
		all.add(meaning);
		if(synonyms.size()>0){
			for(Term t : synonyms){
				all.add(t.getTerm());
			}
		}
		return all;
	}
	
	public void setAllArticles(String arg){
		article = arg;
		for(Term t: synonyms){
			t.setArticle(arg);
		}
	}
	
	public void setArticle(String arg){
		article=arg;
	}
	public String getArticle(){
		return article;
	}
	public boolean hasArticle(){
		return (article!=null && !article.equals("") && !article.equals("~"));
	}
	
	public void setPhonetic(String s){
		phonetic = (s==null)?"~":s;
	}
	public String getPhonetic(){
		return phonetic;
	}
	public boolean hasPhonetic(){
		return (phonetic!=null && !phonetic.equals("~") && !phonetic.equals(""));
	}
	
	public void setNote(String s){
		note = (s==null || s.equals(""))?"~":s;
	}
	public String getNote(){
		return note;
	}
	public boolean hasNote(){
		return (note!=null && !note.equals("~") && !note.equals(""));
	}
	
	public boolean hasMeta(){
		if(!metaDescription.isEmpty()){
			for(String s:metaDescription.values().toArray(new String[metaDescription.keySet().size()])){
				if(s!=null && !s.equals("")) return true;
			}
		}
    	return false;
	}
	
	public boolean hasMetaEntryFor(String key){
		if(!metaDescription.isEmpty()){
			if(metaDescription.containsKey(key)){
				return metaDescription.get(key)!=null && !metaDescription.get(key).equals("");
			}
		}
		return false;
	}
	
	public HashMap<String,String> getMetaDescription(){
		return metaDescription;
	}
	public void setMeta(HashMap<String,String> hm){
		metaDescription = hm;
	}
	public String getMetaElement(String key){
		return metaDescription.get(key);
	}
	public void putMetaElement(String a, String b){
		metaDescription.put(a,b);
	}
	public void delMeta(String key){
		metaDescription.remove(key);
	}
	
	public void addSynonym(String art, String term, String category){
		if(hasSynonyms()){
			for(Term t : synonyms){
				if(t.getTerm().equals(term)) return;
			}
		}
		synonyms.add(new Term(art,term, category));
	}
	
	public void deleteSynonym(String term){
		if(hasSynonyms()){
			int size = synonyms.size();
			for(int i=0;i<size;i++){
				if(synonyms.elementAt(i).getTerm().equals(term)) synonyms.remove(i);
			}
		}
	}
	
	public Vector<Term> getSynonyms(){
		return synonyms;
	}
	
	/**
	 * Generates a String containing all synonyms with article and category separated by comma.
	 * @return (article) meaning [category] or an empty String
	 */
	
	public String getSynonymsAsString(){
		if(hasSynonyms()){
			StringBuffer sb = new StringBuffer();
			int size = synonyms.size();
			for(int i=0;i<size;i++){
				sb.append(synonyms.elementAt(i).getFullTerm()+(synonyms.elementAt(i).hasCategory()?" ["+synonyms.elementAt(i).getCategory()+"]":"")+((i<size-1)?", ":""));
			}
			return sb.toString();
		}
		else return "";
	}
	
	public boolean hasSynonyms(){
		return (synonyms!=null && synonyms.size()>0);
	}
	
	public void setSynonyms(Vector<Term> syns){
		synonyms = syns;
	}
	
	public void setExampleSentence(String txt){
		exampleSentence = txt==null?"":txt;
	}
	
	public void deleteExampleSentence(){
		exampleSentence = "";
	}
	
	public String getExampleSentence(){
		return exampleSentence;
	}
	
	public boolean hasExampleSentence(){
		return (exampleSentence!=null && !exampleSentence.equals("") && !exampleSentence.equals("~"));
	}
	
	public boolean hasCategory(){
		return (category!=null && !category.equals("") && !category.equals("~") && !category.equals("n.a."));
	}
	
	public String getCategory(){
		return category;
	}
	
	public void setCategory(String s){
		category = (s==null||s.equals("")||s.equals("~"))?"n.a.":s;
	}
}
